package com.bwsoft.portal.action;

import java.io.File;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.bwsoft.admin.domain.Account;
import com.bwsoft.admin.domain.Advertisement;
import com.bwsoft.admin.domain.Standard;
import com.bwsoft.api.pojo.ResponsePojo;
import com.bwsoft.portal.domain.valueObject.StandardListVO;
import com.bwsoft.portal.domain.valueObject.StandardVO;

public class StandardAction extends PortalBaseAction {
	
	private static Logger LOG = Logger.getLogger(StandardAction.class);
	
	List<HashMap> standardFieldStr;
	List<HashMap> standardTypeStr;
	List<HashMap> auditStatusStr;
	private String s_type;
	private String s_field;
	private String s_standardName;
	private int pagecount = 0;
	private int pageSize = 0;
	private File stdfile;
	private String stdfileFileName; // 文件名称
	private String standardName;
	private String field;
	private String type;
	private String s_status;
	private Advertisement adLeft;
	private Advertisement adMid;
	private Advertisement adRight;
	//显示类型，0：全部，1：我的标准
	private String showType;
	
	public String index() {
		// 获取标准领域
		ResponsePojo responsePoj= dictionaryService.getData("standard_field");
		List<Map<String, Object>> returnMap = (List<Map<String, Object>>)responsePoj.getData();
		standardFieldStr = (List<HashMap>)returnMap.get(0).get("dicData");
		
		// 获取标准类型
		responsePoj= dictionaryService.getData("standard_type");
		returnMap = (List<Map<String, Object>>)responsePoj.getData();
		standardTypeStr = (List<HashMap>)returnMap.get(0).get("dicData");
		
		long recordcount = 0;
		try {
			if (s_standardName != null && !"".equals(s_standardName)) {
				s_standardName = URLDecoder.decode(s_standardName, "UTF-8");
				s_standardName = URLDecoder.decode(s_standardName, "UTF-8");
			}
			recordcount = standardService.getCount(s_type, s_field, s_standardName, s_status, showType, null);
		} catch (Exception e) {
			LOG.info("查找质量控制出错");
			e.printStackTrace();
		}
		getPageInfo(recordcount);
		
		//获取广告
		adLeft = adService.getAdByPos(31);
		adMid = adService.getAdByPos(32);
		adRight = adService.getAdByPos(33);
		return "standardIndex";
	}

	private void getPageInfo(long recordcount) {
		pagecount = (int)(recordcount % getRows() == 0 ? (recordcount / getRows()) : (recordcount / getRows() + 1));
		
		if (pagecount < 1)
		{
			pageSize = 1;
			pagecount = 1;
		}
		else if (pagecount > 8)
			pageSize = 8;
		else
			pageSize = pagecount;
	}
	
	//上面菜单的标准
	public String getStandardListJson() {
		List<Standard> standardList=null;
		List<StandardListVO> standardVOs = null;
			
		try {
			if (s_standardName != null && !"".equals(s_standardName)) {
				s_standardName = URLDecoder.decode(s_standardName, "UTF-8");
				s_standardName = URLDecoder.decode(s_standardName, "UTF-8");
			}
			standardList = standardService.findData(page, rows, s_type, s_field, s_standardName, s_status, "", null);
			standardVOs = standardService.getListVo(standardList);
		} catch (Exception e) {
			LOG.info("获取标准出错");
			e.printStackTrace();
		}
		
		responseJson.put("rows", standardVOs);
		
		return "mapJsonResult";
	}
	
	//左侧菜单我的标准
	public String getStandardsJson() {
		List<Standard> standardList=null;
		List<StandardVO> standardVOs = null;
			
		try {
			if (s_standardName != null && !"".equals(s_standardName)) {
				s_standardName = URLDecoder.decode(s_standardName, "UTF-8");
				s_standardName = URLDecoder.decode(s_standardName, "UTF-8");
			}
			Object uploader = session.get("Account");
			standardList = standardService.findData(page, rows, s_type, s_field, s_standardName, s_status, showType,
					uploader == null ? null : ((Account)uploader).getAccountId());
			standardVOs = standardService.getVo(standardList);
		} catch (Exception e) {
			LOG.info("获取标准出错");
			e.printStackTrace();
		}
		
		responseJson.put("rows", standardVOs);
		
		return "mapJsonResult";
	}
	
	public String showCreate() {
		// 获取标准领域
		ResponsePojo responsePoj= dictionaryService.getData("standard_field");
		List<Map<String, Object>> returnMap = (List<Map<String, Object>>)responsePoj.getData();
		standardFieldStr = (List<HashMap>)returnMap.get(0).get("dicData");
		
		// 获取标准类型
		responsePoj= dictionaryService.getData("standard_type");
		returnMap = (List<Map<String, Object>>)responsePoj.getData();
		standardTypeStr = (List<HashMap>)returnMap.get(0).get("dicData");
		
		//获取广告
		adLeft = adService.getAdByPos(31);
		adMid = adService.getAdByPos(32);
		adRight = adService.getAdByPos(33);
		
		return "showCreate";
	}
	
	public String doCreate() {
		if (standardName == null || "".equals(standardName)) { 
			responseJson.put("result", 1);
			responseJson.put("msg", "标准名称不能为空！");
			return "mapJsonResult";
		}
		if (stdfileFileName == null || "".equals(stdfileFileName)) { 
			responseJson.put("result", 2);
			responseJson.put("msg", "附件不能为空！");
			return "mapJsonResult";
		}
		String stdfileType = stdfileFileName.substring(stdfileFileName.lastIndexOf(".")).toLowerCase();
		if(stdfileFileName != null && !"".equals(stdfileFileName) && !".pdf".equals(stdfileType)){
			responseJson.put("result", 3);
			responseJson.put("msg", "附件不是PDF类型！");
			return "mapJsonResult";
		}
		if(stdfileFileName != null && !"".equals(stdfileFileName) && !newsService.isFitSize(stdfile)){
			responseJson.put("result", 4);
			responseJson.put("msg", "证件1上传的图片不能超过8M！");
			return "mapJsonResult";
		}
		ResponsePojo rp = standardService.saveOrUpdateStandard(standardName, ((Account)session.get("Account")), 
				field, type, stdfile, stdfileFileName);
		//easyui返回的json的contentType必须为text/html
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		json.put("result", rp.getErrcode());
		json.put("msg", rp.getErrmsg());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
	public String mystandard() {
		// 获取标准领域
		ResponsePojo responsePoj= dictionaryService.getData("standard_field");
		List<Map<String, Object>> returnMap = (List<Map<String, Object>>)responsePoj.getData();
		standardFieldStr = (List<HashMap>)returnMap.get(0).get("dicData");
		
		// 获取标准类型
		responsePoj= dictionaryService.getData("standard_type");
		returnMap = (List<Map<String, Object>>)responsePoj.getData();
		standardTypeStr = (List<HashMap>)returnMap.get(0).get("dicData");
		
		// 获取审核状态
		responsePoj= dictionaryService.getData("audit_status");
		returnMap = (List<Map<String, Object>>)responsePoj.getData();
		auditStatusStr = (List<HashMap>)returnMap.get(0).get("dicData");
		
		long recordcount = 0;
		try {
			if (s_standardName != null && !"".equals(s_standardName)) {
				s_standardName = URLDecoder.decode(s_standardName, "UTF-8");
				s_standardName = URLDecoder.decode(s_standardName, "UTF-8");
			}
			Object uploader = session.get("Account");
			recordcount = standardService.getCount(s_type, s_field, s_standardName, s_status, showType,
					uploader == null ? null : ((Account)uploader).getAccountId());
		} catch (Exception e) {
			LOG.info("查找质量控制出错");
			e.printStackTrace();
		}
		getPageInfo(recordcount);
		return "myStandard";
	}

	public List<HashMap> getStandardFieldStr() {
		return standardFieldStr;
	}

	public void setStandardFieldStr(List<HashMap> standardFieldStr) {
		this.standardFieldStr = standardFieldStr;
	}

	public List<HashMap> getStandardTypeStr() {
		return standardTypeStr;
	}

	public void setStandardTypeStr(List<HashMap> standardTypeStr) {
		this.standardTypeStr = standardTypeStr;
	}

	public String getS_type() {
		return s_type;
	}

	public void setS_type(String s_type) {
		this.s_type = s_type;
	}

	public String getS_field() {
		return s_field;
	}

	public void setS_field(String s_field) {
		this.s_field = s_field;
	}

	public String getS_standardName() {
		return s_standardName;
	}

	public void setS_standardName(String s_standardName) {
		this.s_standardName = s_standardName;
	}

	public int getPagecount() {
		return pagecount;
	}

	public void setPagecount(int pagecount) {
		this.pagecount = pagecount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public File getStdfile() {
		return stdfile;
	}

	public void setStdfile(File stdfile) {
		this.stdfile = stdfile;
	}

	public String getStdfileFileName() {
		return stdfileFileName;
	}

	public void setStdfileFileName(String stdfileFileName) {
		this.stdfileFileName = stdfileFileName;
	}

	public String getStandardName() {
		return standardName;
	}

	public void setStandardName(String standardName) {
		this.standardName = standardName;
	}

	public String getField() {
		return field;
	}

	public void setField(String field) {
		this.field = field;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public List<HashMap> getAuditStatusStr() {
		return auditStatusStr;
	}

	public void setAuditStatusStr(List<HashMap> auditStatusStr) {
		this.auditStatusStr = auditStatusStr;
	}

	public String getS_status() {
		return s_status;
	}

	public void setS_status(String s_status) {
		this.s_status = s_status;
	}

	public Advertisement getAdLeft() {
		return adLeft;
	}

	public void setAdLeft(Advertisement adLeft) {
		this.adLeft = adLeft;
	}

	public Advertisement getAdMid() {
		return adMid;
	}

	public void setAdMid(Advertisement adMid) {
		this.adMid = adMid;
	}

	public Advertisement getAdRight() {
		return adRight;
	}

	public void setAdRight(Advertisement adRight) {
		this.adRight = adRight;
	}

	public String getShowType() {
		return showType;
	}

	public void setShowType(String showType) {
		this.showType = showType;
	}

}
